package com.leonzx.base.utils;

import com.aliyun.oss.OSSClient;
import com.aliyun.oss.model.DownloadFileRequest;
import com.aliyun.oss.model.DownloadFileResult;
import com.aliyun.oss.model.PutObjectResult;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;

/**
 * OSS
 * 工具类
 * <p>
 * Created by liang on 2017/12/15.
 */
public class OSSUtils {
    public static final String FILE_PREFIX = "static" + File.separator + "files";
    /**
     * 检查上传文件到OSS
     *
     * @param inputStream
     * @param key
     * @return
     */
    public static PutObjectResult update2OSS(InputStream inputStream, String key) {
        OSSClient ossClient = new OSSClient(OSSConstants.endpoint, OSSConstants.accessKeyId, OSSConstants.accessKeySecret);
        PutObjectResult por = ossClient.putObject(OSSConstants.bucketName, key, inputStream);
        ossClient.shutdown();
        return por;
    }

    public static PutObjectResult upLoadMultipartFileByUrl(MultipartFile file, String url) throws IOException {
        InputStream is = file.getInputStream();
        PutObjectResult por = update2OSS(is, url.replaceAll("\\\\", "/"));
        return por;
    }

    /**
     * 根据图片的保存路径和名称返回请求的路径
     *
     * @param path
     * @param newname
     * @return
     */
    public static String getUrlByPathAndNewName(String path, String newname) {
        String url = "http://" + OSSConstants.bucketName + "." + OSSConstants.endpoint + "/" + path.replaceAll("\\\\", "/") + "/" + newname ;
        return url;
    }

    /**
     * 根据图片的保存路径和名称设定路径
     *
     * @param path
     * @param newname
     * @return
     */
    public static String setUrlByBcljAndXwjmc(String path, String newname) {
        String url = path.replaceAll("\\\\", "/") + "/" + newname;
        return url;
    }

    /**
     * 根据key删除OSS服务器上的文件
     *
     * @param key Bucket下的文件的路径名+文件名 如："upload/cake.jpg"
     */
    public static void deleteFile(String key) {
        OSSClient ossClient = new OSSClient(OSSConstants.endpoint, OSSConstants.accessKeyId, OSSConstants.accessKeySecret);
        ossClient.deleteObject(OSSConstants.bucketName, key.replaceAll("\\\\", "/"));
        // 关闭client
        ossClient.shutdown();
    }

    /**
     * 判断是否存在指定文件
     *
     * @param url
     * @return
     */
    public static Boolean judgeIsUpload(String url) {
        OSSClient ossClient = new OSSClient(OSSConstants.endpoint, OSSConstants.accessKeyId, OSSConstants.accessKeySecret);
        boolean found = ossClient.doesObjectExist(OSSConstants.bucketName, url);
        // 关闭client
        ossClient.shutdown();
        return found;
    }

    /**
     * 下载图片到指定路径
     *
     * @param key
     * @param fileName
     */
    public static void downloadFile(String key, String fileName) throws Throwable {
        // 创建OSSClient实例
        OSSClient ossClient = new OSSClient(OSSConstants.endpoint, OSSConstants.accessKeyId, OSSConstants.accessKeySecret);
        // 下载请求，10个任务并发下载，启动断点续传
        DownloadFileRequest downloadFileRequest = new DownloadFileRequest(OSSConstants.bucketName, key);
        downloadFileRequest.setDownloadFile(fileName);
        downloadFileRequest.setTaskNum(10);
        downloadFileRequest.setEnableCheckpoint(true);
        // 下载文件
        DownloadFileResult downloadRes = ossClient.downloadFile(downloadFileRequest);
        // 下载成功时，会返回文件的元信息
        downloadRes.getObjectMetadata();
        // 关闭client
        ossClient.shutdown();
    }
}